/**
 * 
 */
package com.mywind.service.windfarm.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mywind.dao.windfarm.CheckTimeDao;
import com.mywind.dao.windfarm.ConsumDao;
import com.mywind.dao.windfarm.GenPowerDao;
import com.mywind.dao.windfarm.RunTimeDao;
import com.mywind.dao.windfarm.UseHourDao;
import com.mywind.dao.windfarm.UseRateDao;
import com.mywind.dao.windfarm.WindFarmDao;
import com.mywind.dto.windfarm.LostGenPowerDTO;
import com.mywind.entity.windfarm.CheckTimeEntity;
import com.mywind.entity.windfarm.ConsumEntity;
import com.mywind.entity.windfarm.GenPowerEntity;
import com.mywind.entity.windfarm.RunTimeEntity;
import com.mywind.entity.windfarm.UseHourEntity;
import com.mywind.entity.windfarm.UseRateEntity;
import com.mywind.service.windfarm.IWindFarmServ;

/**
 * @author a01513
 *
 */
@Service
public class WindFarmServ implements IWindFarmServ {
	
	@Autowired
	private GenPowerDao dao;
	@Autowired
	private WindFarmDao windfarmDao;
	@Autowired
	private ConsumDao consumDao;
	@Autowired
	private RunTimeDao runtimeDao;
	@Autowired
	private UseRateDao userateDao;
	@Autowired
	private CheckTimeDao checkTimeDao;
	@Autowired
	private UseHourDao usehourDao;

	@Override
	public List<GenPowerEntity> getGenPower(String farmid,int ltb,int htb,String start,String end) {
		List<GenPowerEntity> list = new ArrayList<GenPowerEntity>();
		for(int i=ltb; i<=htb; i++){
			GenPowerEntity gpe = new GenPowerEntity();
			List<GenPowerEntity> slist = new ArrayList<GenPowerEntity>();
			if("".equals(start) && !"".equals(end)){
				slist = dao.findGenPowerByTLE(farmid, i, end);
			}else if(!"".equals(start) && "".equals(end)){
				slist = dao.findGenPowerByTEQ(farmid, i, start);
			}else if(!"".equals(start) && !"".equals(end)){
				slist = dao.findGenPowerByTBT(farmid, i, start, end);
			}else
				return null;
			float value=0f;
			float windspeed=0f;
			float unvalue=0f;
			float tvalue=0f;
			float coup=0f;
			for(GenPowerEntity gp:slist){
				value = value + gp.getValue();
				windspeed = windspeed + gp.getWindspeed();
				unvalue = unvalue + gp.getUnvalue();
				tvalue = tvalue + gp.getTvalue();
				coup = coup + gp.getCoup();
			}
			windspeed = windspeed / slist.size();
			gpe.setFarmid(farmid);
			gpe.setTurbineid(i);
			gpe.setValue(value);
			gpe.setUnvalue(unvalue);
			gpe.setTvalue(tvalue);
			gpe.setCoup(coup);
			gpe.setWindspeed(windspeed);
			list.add(gpe);
		}
		return list;
	}

	@Override
	public List<GenPowerEntity> getMutiGenPower(String farmListStr,String start, String end) {
		List<GenPowerEntity> list = new ArrayList<GenPowerEntity>();
		String[] farmList = farmListStr.split(",");
		for(int i=0; i<farmList.length; i++){
			GenPowerEntity gpe = new GenPowerEntity();
			List<GenPowerEntity> slist = new ArrayList<GenPowerEntity>();
			if("".equals(start) && !"".equals(end)){
				slist = dao.findGenPowerByLE(farmList[i], end);
			}else if(!"".equals(start) && "".equals(end)){
				slist = dao.findGenPowerByEQ(farmList[i], start);
			}else if(!"".equals(start) && !"".equals(end)){
				slist = dao.findGenPowerByBT(farmList[i], start, end);
			}else
				return null;
			float value=0f;
			float windspeed=0f;
			float unvalue=0f;
			float tvalue=0f;
			float coup=0f;
			for(GenPowerEntity gp:slist){
				value = value + gp.getValue();
				windspeed = windspeed + gp.getWindspeed();
				unvalue = unvalue + gp.getUnvalue();
				tvalue = tvalue + gp.getTvalue();
				coup = coup + gp.getCoup();
			}
			if(slist!=null && slist.size() > 0)
				windspeed = windspeed / slist.size();
			else
				windspeed = 0;
			gpe.setFarmid(farmList[i]);
			gpe.setFarmname(windfarmDao.findWindFarmByFarmid(farmList[i]).getFarmname());
			gpe.setTurbineid(i);
			gpe.setValue(value);
			gpe.setUnvalue(unvalue);
			gpe.setTvalue(tvalue);
			gpe.setWindspeed(windspeed);
			gpe.setCoup(coup);
			list.add(gpe);
		}
		return list;
	}

	@Override
	public List<LostGenPowerDTO> getMutiLostPower(String farmListStr,String start, String end) {
		List<LostGenPowerDTO> list = new ArrayList<LostGenPowerDTO>();
		String[] farmList = farmListStr.split(",");
		for(int i=0; i<farmList.length; i++){
			LostGenPowerDTO lostPowerDto = new LostGenPowerDTO();
			List<GenPowerEntity> slist = new ArrayList<GenPowerEntity>();
			if("".equals(start) && !"".equals(end)){
				slist = dao.findGenPowerByLE(farmList[i], end);
			}else if(!"".equals(start) && "".equals(end)){
				slist = dao.findGenPowerByEQ(farmList[i], start);
			}else if(!"".equals(start) && !"".equals(end)){
				slist = dao.findGenPowerByBT(farmList[i], start, end);
			}else
				return null;
			float value=0f;
			float unvalue=0f;
			float rate=0f;
			for(GenPowerEntity gp:slist){
				value = value + gp.getValue();
				unvalue = unvalue + gp.getUnvalue();
			}
			lostPowerDto.setFarmid(farmList[i]);
			lostPowerDto.setFarmname(windfarmDao.findWindFarmByFarmid(farmList[i]).getFarmname());
			lostPowerDto.setValue(value);
			lostPowerDto.setUnvalue(unvalue);
			if(value!=0)
				rate = unvalue / value;
			else
				rate = 0;
			lostPowerDto.setRate(Math.abs(rate));
			list.add(lostPowerDto);
		}
		return list;
	}

	@Override
	public List<ConsumEntity> getMutiConsum(String farmListStr, String start,String end) {
		List<ConsumEntity> list = new ArrayList<ConsumEntity>();
		String[] farmList = farmListStr.split(",");
		for(int i=0; i<farmList.length; i++){
			ConsumEntity consumEntity = new ConsumEntity();
			List<ConsumEntity> slist = new ArrayList<ConsumEntity>();
			if("".equals(start) && !"".equals(end)){
				slist = consumDao.findConsumByLE(farmList[i], end);
			}else if(!"".equals(start) && "".equals(end)){
				slist = consumDao.findConsumByEQ(farmList[i], start);
			}else if(!"".equals(start) && !"".equals(end)){
				slist = consumDao.findConsumByBT(farmList[i], start, end);
			}else
				return null;
			float gvalue=0f;
			float cvalue=0f;
			float rate=0f;
			for(ConsumEntity ce:slist){
				gvalue = gvalue + ce.getGvalue();
				cvalue = cvalue + ce.getCvalue();
			}
			consumEntity.setFarmid(farmList[i]);
			consumEntity.setFarmname(windfarmDao.findWindFarmByFarmid(farmList[i]).getFarmname());
			consumEntity.setCvalue(cvalue);
			consumEntity.setGvalue(gvalue);
			if(gvalue!=0)
				rate = cvalue / gvalue;
			else
				rate = 0;
			consumEntity.setRate(Math.abs(rate));
			list.add(consumEntity);
		}
		return list;
	}

	@Override
	public List<RunTimeEntity> getRunTime(String farmid, int ltb, int htb,String start, String end) {
		List<RunTimeEntity> list = new ArrayList<RunTimeEntity>();
		for(int i=ltb; i<=htb; i++){
			RunTimeEntity gpe = new RunTimeEntity();
			List<RunTimeEntity> slist = new ArrayList<RunTimeEntity>();
			if("".equals(start) && !"".equals(end)){
				slist = runtimeDao.findRunTimeByTLE(farmid, i, end);
			}else if(!"".equals(start) && "".equals(end)){
				slist = runtimeDao.findRunTimeByTEQ(farmid, i, start);
			}else if(!"".equals(start) && !"".equals(end)){
				slist = runtimeDao.findRunTimeByTBT(farmid, i, start, end);
			}else
				return null;
			int rvalue=0;
			for(RunTimeEntity gp:slist){
				rvalue = rvalue + gp.getRvalue();
			}
			rvalue = rvalue / 3600;
			gpe.setFarmid(farmid);
			gpe.setTurbineid(i);
			gpe.setRvalue(rvalue);
			list.add(gpe);
		}
		return list;
	}

	@Override
	public List<RunTimeEntity> getMutiRunTime(String farmListStr, String start,String end) {
		List<RunTimeEntity> list = new ArrayList<RunTimeEntity>();
		String[] farmList = farmListStr.split(",");
		for(int i=0; i<farmList.length; i++){
			RunTimeEntity gpe = new RunTimeEntity();
			List<RunTimeEntity> slist = new ArrayList<RunTimeEntity>();
			if("".equals(start) && !"".equals(end)){
				slist = runtimeDao.findRunTimeByLE(farmList[i], end);
			}else if(!"".equals(start) && "".equals(end)){
				slist = runtimeDao.findRunTimeByEQ(farmList[i], start);
			}else if(!"".equals(start) && !"".equals(end)){
				slist = runtimeDao.findRunTimeByBT(farmList[i], start, end);
			}else
				return null;
			int rvalue=0;
			for(RunTimeEntity gp:slist){
				rvalue = rvalue + gp.getRvalue();
			}
			rvalue = rvalue / 3600;
			gpe.setFarmid(farmList[i]);
			gpe.setFarmname(windfarmDao.findWindFarmByFarmid(farmList[i]).getFarmname());
			gpe.setTurbineid(i);
			gpe.setRvalue(rvalue);
			list.add(gpe);
		}
		return list;
	}

	@Override
	public List<UseRateEntity> getUseRate(String farmid, int ltb, int htb,String start, String end) {
		List<UseRateEntity> list = new ArrayList<UseRateEntity>();
		for(int i=ltb; i<=htb; i++){
			UseRateEntity gpe = new UseRateEntity();
			long searchTime=0l;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			List<UseRateEntity> slist = new ArrayList<UseRateEntity>();
			if("".equals(start) && !"".equals(end)){
				slist = userateDao.findUseRateByTLE(farmid, i, end);
				try {
					Date date = sdf.parse(end);
					searchTime = date.getTime() - sdf.parse("2014-11-01").getTime();
					if(searchTime < 0)
						searchTime = 0;
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}else if(!"".equals(start) && "".equals(end)){
				slist = userateDao.findUseRateByTEQ(farmid, i, start);
				searchTime = 24 * 60 * 60 * 1000;
			}else if(!"".equals(start) && !"".equals(end)){
				slist = userateDao.findUseRateByTBT(farmid, i, start, end);
				try {
					searchTime = sdf.parse(end).getTime() - sdf.parse(start).getTime();
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}else
				return null;
			float fvalue=0f;
			int fnum=0;
			float rate=0f;
			float fpower=0f;
			for(UseRateEntity gp:slist){
				fvalue = fvalue + gp.getFvalue();
				fnum = fnum + gp.getFnum();
				fpower = fpower + gp.getFpower();
			}
			if(searchTime > 0)
				rate = (searchTime/1000 - fvalue) / (searchTime/1000);
			else
				rate = 1;
			gpe.setFarmid(farmid);
			gpe.setTurbineid(i);
			gpe.setFvalue(fvalue/3600);
			gpe.setFnum(fnum);
			gpe.setRate(rate);
			gpe.setFpower(fpower);
			list.add(gpe);
		}
		return list;
	}

	@Override
	public List<UseRateEntity> getMutiUseRate(String farmListStr, String start,String end) {
		List<UseRateEntity> list = new ArrayList<UseRateEntity>();
		String[] farmList = farmListStr.split(",");
		for(int i=0; i<farmList.length; i++){
			UseRateEntity gpe = new UseRateEntity();
			long searchTime=0l;
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			List<UseRateEntity> slist = new ArrayList<UseRateEntity>();
			if("".equals(start) && !"".equals(end)){
				slist = userateDao.findUseRateByLE(farmList[i], end);
				try {
					Date date = sdf.parse(end);
					searchTime = date.getTime() - sdf.parse("2014-11-01").getTime();
					if(searchTime < 0)
						searchTime = 0;
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}else if(!"".equals(start) && "".equals(end)){
				slist = userateDao.findUseRateByEQ(farmList[i], start);
				searchTime = 24 * 60 * 60 * 1000;
			}else if(!"".equals(start) && !"".equals(end)){
				slist = userateDao.findUseRateByBT(farmList[i], start, end);
				try {
					searchTime = sdf.parse(end).getTime() - sdf.parse(start).getTime();
				} catch (ParseException e) {
					e.printStackTrace();
				}
			}else
				return null;
			float fvalue=0f;
			int fnum=0;
			float rate=0f;
			float fpower=0f;
			for(UseRateEntity gp:slist){
				fvalue = fvalue + gp.getFvalue();
				fnum = fnum + gp.getFnum();
				fpower = fpower + gp.getFpower();
			}
			if(searchTime > 0)
				rate = (searchTime/1000 - fvalue) / (searchTime/1000);
			else
				rate = 1;
			gpe.setFarmid(farmList[i]);
			gpe.setFarmname(windfarmDao.findWindFarmByFarmid(farmList[i]).getFarmname());
			gpe.setTurbineid(i);
			gpe.setFvalue(fvalue/3600);
			gpe.setFnum(fnum);
			gpe.setRate(rate);
			gpe.setFpower(fpower);
			list.add(gpe);
		}
		return list;
	}

	@Override
	public List<CheckTimeEntity> getCheckTime(String farmid, int ltb, int htb,String start, String end) {
		List<CheckTimeEntity> list = new ArrayList<CheckTimeEntity>();
		for(int i=ltb; i<=htb; i++){
			CheckTimeEntity gpe = new CheckTimeEntity();
			List<CheckTimeEntity> slist = new ArrayList<CheckTimeEntity>();
			if("".equals(start) && !"".equals(end)){
				slist = checkTimeDao.findCheckTimeByTLE(farmid, i, end);
			}else if(!"".equals(start) && "".equals(end)){
				slist = checkTimeDao.findCheckTimeByTEQ(farmid, i, start);
			}else if(!"".equals(start) && !"".equals(end)){
				slist = checkTimeDao.findCheckTimeByTBT(farmid, i, start, end);
			}else
				return null;
			int rvalue=0;
			for(CheckTimeEntity gp:slist){
				rvalue = rvalue + gp.getRvalue();
			}
			gpe.setFarmid(farmid);
			gpe.setTurbineid(i);
			gpe.setRvalue(rvalue/3600);
			list.add(gpe);
		}
		return list;
	}

	@Override
	public List<CheckTimeEntity> getMutiCheckTime(String farmListStr,String start, String end) {
		List<CheckTimeEntity> list = new ArrayList<CheckTimeEntity>();
		String[] farmList = farmListStr.split(",");
		for(int i=0; i<farmList.length; i++){
			CheckTimeEntity gpe = new CheckTimeEntity();
			List<CheckTimeEntity> slist = new ArrayList<CheckTimeEntity>();
			if("".equals(start) && !"".equals(end)){
				slist = checkTimeDao.findCheckTimeByLE(farmList[i], end);
			}else if(!"".equals(start) && "".equals(end)){
				slist = checkTimeDao.findCheckTimeByEQ(farmList[i], start);
			}else if(!"".equals(start) && !"".equals(end)){
				slist = checkTimeDao.findCheckTimeByBT(farmList[i], start, end);
			}else
				return null;
			int rvalue=0;
			for(CheckTimeEntity gp:slist){
				rvalue = rvalue + gp.getRvalue();
			}
			gpe.setFarmid(farmList[i]);
			gpe.setFarmname(windfarmDao.findWindFarmByFarmid(farmList[i]).getFarmname());
			gpe.setTurbineid(i);
			gpe.setRvalue(rvalue/3600);
			list.add(gpe);
		}
		return list;
	}

	@Override
	public List<UseHourEntity> getUseHour(String farmid, int ltb, int htb,String start, String end) {
		List<UseHourEntity> list = new ArrayList<UseHourEntity>();
		for(int i=ltb; i<=htb; i++){
			UseHourEntity gpe = new UseHourEntity();
			List<UseHourEntity> slist = new ArrayList<UseHourEntity>();
			if("".equals(start) && !"".equals(end)){
				slist = usehourDao.findUseHourByTLE(farmid, i, end);
			}else if(!"".equals(start) && "".equals(end)){
				slist = usehourDao.findUseHourByTEQ(farmid, i, start);
			}else if(!"".equals(start) && !"".equals(end)){
				slist = usehourDao.findUseHourByTBT(farmid, i, start, end);
			}else
				return null;
			int rvalue=0;
			for(UseHourEntity gp:slist){
				rvalue = rvalue + gp.getRvalue();
			}
			gpe.setFarmid(farmid);
			gpe.setTurbineid(i);
			gpe.setRvalue(rvalue/3600);
			list.add(gpe);
		}
		return list;
	}

	@Override
	public List<UseHourEntity> getMutiUseHour(String farmListStr, String start,String end) {
		List<UseHourEntity> list = new ArrayList<UseHourEntity>();
		String[] farmList = farmListStr.split(",");
		for(int i=0; i<farmList.length; i++){
			UseHourEntity gpe = new UseHourEntity();
			List<UseHourEntity> slist = new ArrayList<UseHourEntity>();
			if("".equals(start) && !"".equals(end)){
				slist = usehourDao.findUseHourByLE(farmList[i], end);
			}else if(!"".equals(start) && "".equals(end)){
				slist = usehourDao.findUseHourByEQ(farmList[i], start);
			}else if(!"".equals(start) && !"".equals(end)){
				slist = usehourDao.findUseHourByBT(farmList[i], start, end);
			}else
				return null;
			int rvalue=0;
			for(UseHourEntity gp:slist){
				rvalue = rvalue + gp.getRvalue();
			}
			gpe.setFarmid(farmList[i]);
			gpe.setFarmname(windfarmDao.findWindFarmByFarmid(farmList[i]).getFarmname());
			gpe.setTurbineid(i);
			gpe.setRvalue(rvalue/3600);
			list.add(gpe);
		}
		return list;
	}
	
}
